#include "Texture3Dscene.h"

#include "rawreader.h"
//#include "RawDataProcessor.h"
#include "TranformationMgr.h"



Texture3Dscene::Texture3Dscene(void):
	m_pRawDataProc(0)
{
	dOrthoSize = 1.0f;
	m_pTransformMgr = new CTranformationMgr;
}

Texture3Dscene::~Texture3Dscene(void)
{
}

// Macro to draw the quad.
// Performance can be achieved by making a call list.
// To make it simple i am not using that now :-)
#define MAP_3DTEXT( TexIndex ) \
	glTexCoord3f(0.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
	glVertex3f(-dOrthoSize,-dOrthoSize,TexIndex);\
	glTexCoord3f(1.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f);  \
	glVertex3f(dOrthoSize,-dOrthoSize,TexIndex);\
	glTexCoord3f(1.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
	glVertex3f(dOrthoSize,dOrthoSize,TexIndex);\
	glTexCoord3f(0.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f);  \
	glVertex3f(-dOrthoSize,dOrthoSize,TexIndex);

void Texture3Dscene::RenderContent()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	for ( float fIndx = -1.0f; fIndx <= 1.0f; fIndx+=0.01f )
	{
		glBegin(GL_QUADS);
		MAP_3DTEXT( fIndx );
		glEnd();
	}

	glFlush();
}

void Texture3Dscene::Init()
{
	glTexImage3D = reinterpret_cast< PFNGLTEXIMAGE3DPROC>( ::wglGetProcAddress( "glTexImage3D") );

	glClearColor (0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	glGenTextures(1, &texName);
	glBindTexture(GL_TEXTURE_3D, texName);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);

	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 
		this->m_uImageWidth, 
		this->m_uImageHeight,
		this->m_uImageCount, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixeldata);//-------------

	//glBindTexture( GL_TEXTURE_3D, 0 );


	//float fFrameCount = (float)m_pRawDataProc->GetDepth();
	glClear( GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT );

	glEnable( GL_ALPHA_TEST );
	glAlphaFunc( GL_GREATER, 0.05f );

	glEnable(GL_BLEND);
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

	glMatrixMode( GL_TEXTURE );
	glLoadIdentity();

	// Translate and make 0.5f as the center 
	// (texture co ordinate is from 0 to 1. so center of rotation has to be 0.5f)
	glTranslatef( 0.5f, 0.5f, 0.5f );

	// A scaling applied to normalize the axis 
	// (Usually the number of slices will be less so if this is not - 
	// normalized then the z axis will look bulky)
	// Flipping of the y axis is done by giving a negative value in y axis.
	// This can be achieved either by changing the y co ordinates in -
	// texture mapping or by negative scaling of y axis
	glScaled( (float)256/(float)256, 
		-1.0f*(float)256/(float)(float)256, 
		(float)256/(float)109);

	//CTranformationMgr* m_pTransform = new CTranformationMgr;
	// Apply the user provided transformations
	glMultMatrixd( m_pTransformMgr->GetMatrix());

	glTranslatef( -0.5f,-0.5f, -0.5f );

	glEnable(GL_TEXTURE_3D);
}

void Texture3Dscene::SetData(rawreader *data)
{
	this->m_uImageWidth = data->GetWidth();
	this->m_uImageHeight = data->GetHeight();
	this->pixeldata = data->GetOutput();
	this->m_uImageCount = data->GetDepth();
}